package org.jeecg.modules.wmhb.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.wmhb.entity.WmContractDep;
import org.jeecg.modules.wmhb.service.IWmContractDepService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.wmhb.service.IWmItemService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 合同产品与事业部映射（分配给事业部的合同产品）
 * @Author: jeecg-boot
 * @Date: 2024-09-05
 * @Version: V1.0
 */
@Api(tags = "合同产品与事业部映射（分配给事业部的合同产品）")
@RestController
@RequestMapping("/wmhb/wmContractDep")
@Slf4j
public class WmContractDepController extends JeecgController<WmContractDep, IWmContractDepService> {
    @Autowired
    private IWmContractDepService wmContractDepService;

    @Autowired
    private IWmItemService wmItemService;

    /**
     * @param wmContractId
     * @param buId
     * 查询事业部分配的产品
     *
     * @return
     */
    @RequestMapping(value = "/queryBuContractItem", method = RequestMethod.GET)
    @ApiOperation(value = "合同产品与事业部映射-查询事业部分配的产品", notes = "合同产品与事业部映射-查询事业部分配的产品")
    public Result<List<String>> queryBuItem(@RequestParam(name = "wmContractId", required = true) String wmContractId, @RequestParam(name = "buId", required = false) String buId) {
        Result<List<String>> result = new Result<>();
        try {
            List<WmContractDep> list = new ArrayList<>();
            if(StringUtils.isEmpty(buId)){
                list = wmContractDepService.list(new QueryWrapper<WmContractDep>().lambda().eq(WmContractDep::getWmContractId, wmContractId));
            }
            else{
                list = wmContractDepService.list(new QueryWrapper<WmContractDep>().lambda().eq(WmContractDep::getBuId, buId).eq(WmContractDep::getWmContractId, wmContractId));
            }

            result.setResult(list.stream().map(wmContractDep -> String.valueOf(wmContractDep.getWmContractProductId())).collect(Collectors.toList()));
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return result;
    }

    /**
     * 保存事业部分配的产品
     *
     * @return
     */
    @RequestMapping(value = "/saveBuContractItem", method = RequestMethod.POST)
    @RequiresPermissions("wmhb:wm_contract_dep:edit")
    @ApiOperation(value = "合同产品与事业部映射-保存事业部分配的产品", notes = "合同产品与事业部映射-保存事业部分配的产品")
    public Result<String> saveBuItem(@RequestBody JSONObject json) {
        long start = System.currentTimeMillis();
        Result<String> result = new Result<>();
        try {
            String wmContractId = json.getString("wmContractId");
            String buId = json.getString("buId");
            String wmContractProductIds = json.getString("wmContractProductIds");
            String lastWmContractProductIds = json.getString("lastWmContractProductIds");
            wmContractDepService.saveBuItem(wmContractId, buId, wmContractProductIds, lastWmContractProductIds);
            result.success("保存成功！");
        } catch (Exception e) {
            result.error500("保存失败！");
            log.error(e.getMessage(), e);
        }
        return result;
    }

}
